import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/a.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/utils/cash_utils.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/common/tag_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';

class TransactionRecordItem extends StatelessWidget {
  final VoidCallback onTap;
  final String tag;
  final String name;
  final String time;
  final num amount;
  final bool overdueDay;
  final String billNumber;
  final String status;
  final String currencyCode;
  final bool isUnpaid;

  TransactionRecordItem(
      {Key key,
      this.onTap,
      this.tag,
      this.isUnpaid,
      this.overdueDay,
      this.name,
      this.billNumber,
      this.time,
      this.status,
      this.amount,
      this.currencyCode})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isReduce = false;
    bool isHideOperation = false;
    String _tag = tag;
    if (tag == 'Transfer Out') {
      isReduce = true;
      _tag = 'Transfer';
    } else if (tag == 'Transfer In') {
      isReduce = false;
      _tag = 'Transfer';
    } else {
      isReduce = (tag == 'Money-Out' ||
          tag == 'Expense' ||
          tag == 'PO' ||
          tag == 'Bill');
    }
    return PressedOpacityButton(
      onPressed: onTap,
      color: Colors.white,
      border:
          Border(bottom: BorderSide(color: aceGrayColor, width: getDp(0.5))),
      child: Container(
        constraints: BoxConstraints(
          minHeight: getDp(90),
        ),
        alignment: Alignment.centerLeft,
        padding: EdgeInsets.symmetric(vertical: getDp(23)),
        child: Stack(
          clipBehavior: Clip.none,
          children: [
            Container(
              constraints: BoxConstraints(
                  minWidth: MediaQuery.of(context).size.width,
                  minHeight: getDp(60)),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  SizedBox(
                    width: getDp(75),
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        Container(
                          alignment: Alignment.center,
                          child: SvgPicture.asset(getImage(_tag),
                              width: 14, height: 17),
                        ),
                        SizedBox(
                          height: getDp(4),
                        ),
                        Text(
                          LanguageUtils.translateTag(_tag),
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              color: aceDarkGrayColor,
                              fontSize: getSp(12),
                              height: 14 / 12),
                        ),
                      ],
                    ),
                  ),
                  Expanded(
                      child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        constraints: BoxConstraints(
                          maxWidth: getDp(180),
                        ),
                        child: Text(
                          name ?? '',
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: getSp(16),
                            height: 14 / 16,
                          ),
                        ),
                      ),
                      if (billNumber != null)
                        Padding(
                          padding:
                              EdgeInsets.only(bottom: getDp(6), top: getDp(6)),
                          child: Text(
                            '${_tag.replaceAll('\n', '')} NO:$billNumber',
                            style: TextStyle(
                              fontWeight: FontWeight.w400,
                              fontSize: getSp(14),
                            ),
                          ),
                        ),
                      Text(
                        '${_tag == 'Expense' && isUnpaid == true ? '${S.current.due}: ' : '${S.current.date}: '}$time',
                        style: TextStyle(
                          fontWeight: FontWeight.w400,
                          fontSize: getSp(14),
                        ),
                      ),
                    ],
                  )),
                ],
              ),
            ),
            Positioned(
              right: 0.0,
              top: 0.0,
              bottom: 0.0,
              child: Padding(
                padding: EdgeInsets.only(right: getDp(19), left: getDp(10)),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    if (isHideTag(_tag))
                      Container()
                    else if (status != null)
                      Container(
                        margin: EdgeInsets.only(
                          bottom: getDp(10),
                        ),
                        child: status == 'Overdue'
                            ? RedTagWidget(
                                content: status,
                              )
                            : NormalRedTagWidget(
                                content: status,
                              ),
                      ),
                    if (overdueDay == true)
                      Container(
                        margin: EdgeInsets.only(
                          bottom: getDp(10),
                        ),
                        child: RedTagWidget(
                          content: 'Overdue',
                        ),
                      ),
                    Text.rich(
                      TextSpan(
                          text: isHideOperation
                              ? ''
                              : isReduce
                                  ? '-'
                                  : '+',
                          style: TextStyle(
                              fontSize: getSp(16),
                              color: CashUtils.getColor(isReduce ? -1 : 1),
                              fontWeight: FontWeight.bold,
                              height: 14 / 12,
                              fontFamily: "DinRoundPro"),
                          children: [
                            TextSpan(
                              text:
                                  ' ${currencyCode ?? 'SGD'} ${CashUtils.ignoreFormatCashAmount(amount)}',
                              style: TextStyle(
                                color: aceBlackColor,
                                fontSize: getSp(16),
                                fontWeight: FontWeight.w700,
                                height: 14 / 16,
                              ),
                            ),
                          ]),
                      textAlign: TextAlign.end,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  bool isHideTag(String tag) {
    return tag == 'Expense' && status == 'Paid' ||
        tag == 'PO' && status == 'Closed' ||
        tag == 'Money-Out' && status == 'Paid' ||
        tag == 'Money-In' && status == 'Paid' ||
        tag == 'Quotation' && status == 'Closed' ||
        tag == 'Invoice' && status == 'Paid' ||
        tag == 'Bill' && status == 'Paid';
  }

  String getImage(String tag) {
    switch (tag) {
      case 'Invoice':
        return A.assetsImagesDashboardInvoice;
      case 'Bill':
        return A.assetsImagesDashboardBill;
      case 'PO':
        return A.assetsImagesDashboardPO;
      case 'purchase_order':
        return A.assetsImagesDashboardPO;
      case 'Money-In':
        return A.assetsImagesDashboardCashIn;
      case 'Money-Out':
        return A.assetsImagesDashboardCash_Out;
      case 'Expense':
        return A.assetsImagesDashboardExpense;
      case 'Quotation':
        return A.assetsImagesDashboardAddQuotation;
      case 'Transfer':
        return A.assetsImagesStyleAccountTransfer;
      case 'Credit \nNote':
        return A.assetsImagesDashboardAddCreditNote;
      case 'Debit \nNote':
        return A.assetsImagesDashboardAddDebitNote;
    }
    return '';
  }
}
